package site.assad.dynamicProxy;

/**
 * Author: Al-assad 余林颖
 * E-mail: yulinying_1994@outlook.com
 * Date: 2017/12/4 23:25
 * Description: 性能管理器类，通过该类管理 MethodPerformance 实例，将 MethodPerformance 实现为单例模式
 */
public class PerformanceMonitor {

    //通过一个 ThreadLocal 类将非线程安全的 MethodPerformanace 实现为线程安全的调用方式
    private static ThreadLocal<MethodPerformance> performanceRecord = new ThreadLocal<>();

    //开始测试
    public static void begin(String method){
        System.out.println("begin monitor ...");
        MethodPerformance mp = performanceRecord.get();
        if(mp == null){
            mp = new MethodPerformance(method);
            performanceRecord.set(mp);
        }else{
            mp.reset(method);
        }
    }

    //结束测试
    public static void end(){
        System.out.println("end monitor...");
        MethodPerformance mp = performanceRecord.get();
        mp.printPerformance();
    }


}
